<?php
// +----------------------------------------------------------------------
// | 迁于首页
// +----------------------------------------------------------------------
// | Author: cart <469714167@qq.com>
// +----------------------------------------------------------------------
// | Datetime: 2018-5-16 14:59:00
// +----------------------------------------------------------------------
// | Copyright: 创讯科技
// +----------------------------------------------------------------------
namespace app\qianyu\controller;
use \think\Controller;
class Index extends Controller
{
    // private $IndexModel;
    /**
     * [_initialize 初始化]
     * @return [type] [description]
     * @author carl
     * @version 2018-02-05
     * @copyright wei liang
     */
    protected function _initialize()
    {
        parent::_initialize();
        $this->UserModel = Loader::model('User');
    }

    /**
     * [findex 微信车保点点绑定oauth跳转入口]
     * 
     * @return [type] [description]
     * @author carl
     * @version 2018-02-05
     * @copyright wei liang
     */
    
    /**
     * [query 提交查询]
     * @return [type] [description]
     */
    public function query()
    {
        $param = input('post.');
        $result = $this->validate($param, 'IndexValidate.query');
            if (true !== $result) {
                return ['code'=>40005,'msg'=>$result,'data'=>[],'status'=>false];
            }
        // $option['engineno'] = 'LVHFR2856J6007094';
        // $option['frameno'] = '1007098';
        // $option['iscity'] = 0;
        // $option['lsEnPrefix'] = 'A';
        // $option['lsnum'] = '58B39';
        // $option['lsprefix'] = urlencode('沪');
        $param['lsprefix'] = urlencode($param['lsprefix']);
        $param['lsnum'] = $param['lsEnPrefix'].$param['lsnum'];
        $res = getIllegalInquiry($param);
        if($res){
            $objData = json_decode($res);
            $res = json_encode($objData->result);
            return ['data' => $res, 'code' => 200, 'message' => '列表显示成功', 'status' => true];
        }else{
            return ['data' => $res, 'code' => 400, 'message' => '列表显示成功', 'status' => false];
        }
    }


    public function userAuthSlogin(){
    $content = file_get_contents ( 'php://input' );
    $content=json_decode($content,true);
    $utoken=$content["utoken"];
    if(!empty($utoken)&&S($utoken)){
      $result["success"]=1;
      $result['utoken']=$utoken;
      echo json_encode($result);
      exit();
    }
    $code=$content["code"];
    $encryptedData=$content["encryptedData"];
    $iv = $content['iv'];
    /*获取session_key*/
    $s_result=$this->getSession($code);
    $WxData = new WXBizDataCrypt($s_result['appid'],$s_result['session_key']);
    /*解密用户数据*/
    $errCode = $WxData->decryptData($encryptedData, $iv, $user_data);
    $wxap_key = md5(uniqid(md5(microtime(true)),true));
    $result=array();
    if($errCode==0){
      $user_data=json_decode($user_data,true);
      $result["success"]=1;
      $result['utoken']=$wxap_key;
      $user_id = $this->wxUserAdd($user_data);
      if($user_id < 1 || empty($user_id)){
        $result["success"]=-1;
        $result['errCode']=0;
        $result['msg']="获取用户信息出错！";
        echo json_encode($result);
        exit();
      }
      $user_data['uid']=$user_id;
      S($wxap_key,$user_data,7200);
      echo json_encode($result);
      exit();
    }else{
      $result["success"]=-1;
      $result['errCode']=$errCode;
      $result['msg']="获取用户信息出错！";
      echo json_encode($result);
      exit();
    }
  }

    /**
     * [getLicensePlate 获取车牌地区信息]
     * @return [res] array [地区信息]
     */
    public function getLicensePlate(){
        $param = input('post.');
        $result = $this->validate($param, 'IndexValidate.level');
            if (true !== $result) {
                return ['code'=>400,'message'=>$result,'data'=>[],'status'=>false];
            }
        $AreaModel = new Area();
        $res = $AreaModel->getListData($param['level']);
        return ['data' => $res, 'code' => 200, 'message' => '列表显示成功', 'status' => true ];
    }

    public function onlogin(){
        $code = input('get.code');
        $appid= 'wxa20052cca5271e47';
        $secret= 'e66c2c6abc2502832a62e4bbb3e708d6';
        $url = 'https://api.weixin.qq.com/sns/jscode2session?appid='.$appid.'&secret='.$secret.'&js_code='.$code.'&grant_type=authorization_code';
        $result = http_get($url);
        if ($result)
        {
            $json = json_decode($result,true);
            if (!$json || isset($json['errcode'])) {
                $errCode = $json['errcode'];
                $errMsg = $json['errmsg'];
                return false;
            }
            $session_key = $json['session_key'];
            $expire = $json['expires_in'] ? intval($json['expires_in'])-100 : 3600;
            $this->setCache($authname,$session_key,$expire);
            $userInfo = $this->UserModel->getUserInfo($json['openid']);
            if(!$userInfo){
                $this->UserModel->openid = $json['openid'];
                $this->UserModel->save();
            }
            return $session_key;
        }
        // return false;
    }

    protected function setCache($cachename,$value,$expired){
        //TODO: set cache implementation
        return Cache::set($cachename,$value,$expired);
    }

}